import Vue from 'vue'
import Vuex from 'vuex'
import createLogger from "vuex/dist/logger";
import createPersistedState from "vuex-persistedstate";
import $router from '../router/index'

// 导入请求方法
import { shop_favlist, shop_fav, shop_unfav, shopcar_list, shopcar_clear, shopcar_add, shopcar_sub, address_list, address_edit } from '../utils/service'
import { Toast } from 'vant';
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    collectlist: [],//商品详情列表
    shopcarlist: [],//购物车商品列表
    addresslist: []//我的收货地址列表
    // alterlist: []
  },
  getters: {
  },
  mutations: {
    collect(state, payload) { //收藏一个店铺
      state.collectlist.push(payload);
    },
    cancel_collect(state, payload) { //取消收藏一个店铺
      var index = state.collectlist.findIndex((item) => {
        return item.mtWmPoiId == payload.mtWmPoiId;
      })
      if (index != -1) {
        state.collectlist.splice(index, 1);
      }
    },
    // 定义一个同步方法，用于异步方法调用修改state中的值
    change_favlist(state, payload) {
      state.collectlist = payload//获取用户的收藏列表直接覆盖vuex的数据
    },
    // 获取购物车商品存在vuex中
    change_shopcarlist(state, payload) {
      state.shopcarlist = payload
    },
    // 清空购物车的同步方法
    clearcar(state) {
      state.shopcarlist = []
    },
    // 购物车加一
    add_shopcar(state, payload) {
      var index = state.shopcarlist.findIndex((item) => {
        return item.id == payload.id
      })
      if (index == -1) {
        var good = JSON.parse(JSON.stringify(payload))
        good.count = 1
        state.shopcarlist.push(good)
      }
      else {
        state.shopcarlist[index].count++
      }
    },
    // 购物车减一
    sub_shopcar(state, payload) {
      var subindex = state.shopcarlist.findIndex((item) => {
        return item.id == payload.id
      })
      if (subindex != -1) {
        if (state.shopcarlist[subindex].count >= 1) {
          state.shopcarlist[subindex].count--
        }
        if (state.shopcarlist[subindex].count == 0) {
          state.shopcarlist.splice(subindex, 1)
        }
      } else {
        // 深拷贝该对象，将深拷贝的对象添加count属性 值为1
        var good = JSON.parse(JSON.stringify(payload))
        good.count = 1
        state.shopcarlist.push(good)
      }
    },
    // 新增地址添加到vuex中
    // address_list(state, payload) {
    //   state.addresslist.push(JSON.parse(payload))
    // },
    add_address(state, payload) {
      state.addresslist.push(JSON.parse(payload))
    },
    address_all(state, payload) {
      state.addresslist = payload
    },
    edit_address(state, payload) {
      var index = state.addresslist.findIndex((item) => {
        return payload.id == item.id
      })
      console.log(state.addresslist)
      state.addresslist.splice(index, 1, payload)
      Toast.success("地址修改成功")
      $router.push('/address')
    }

  },
  actions: {
    // 发送请求是异步方法，定义在actions中
    // 定义发送请求的方法
    request_shop_favlist(store) {
      // 获取本地存储中用户的id
      //用户是字符串形式，需要转换成对象
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      shop_favlist({ userid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('change_favlist', res.data.list)
        }
      })
    },
    // 获取发送收藏店铺
    request_shop_fav(store, payload) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id//获取用户id
      //发请求给服务器店铺收藏列表
      shop_fav({ userid, shopid: payload.mtWmPoiId }).then((res) => {
        if (res.data.code) {
          store.commit('collect', payload)
        }
      })
    },
    // 获取发送取消收藏店铺
    request_shop_unfav(store, payload) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      // 发请求给服务器取消店铺收藏列表
      shop_unfav({ userid, shopid: payload.mtWmPoiId }).then((res) => {
        if (res.data.code == 200) {
          store.commit('cancel_collect', payload)
        }
      })
    },
    // 获取购物车列表
    request_shopcarlist(store) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      shopcar_list({ userid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('change_shopcarlist', res.data.list)
        }
      })
    },
    // 发请求获取地址列表
    request_address(store) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      address_list({ userid }).then((res) => {
        console.log(res.data.list)
        if (res.data.code == 200) {
          store.commit('address_all', res.data.list)
        }
      })
    },
    // 清空购物车
    clear_shopcar(store) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      shopcar_clear({ userid }).then((res) => {
        if (res.data.code == 200) {
          store.commit('clearcar')
        }
      })
    },
    // 购物车商品加一
    request_addshopcar(store, payload) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      shopcar_add({ userid, goodid: payload.id }).then((res) => {
        if (res.data.code == 200) {
          store.commit('add_shopcar', payload)
          // console.log(res.data)
        }
      })
    },
    // 购物车商品减一
    request_subshopcar(store, payload) {
      var userid = JSON.parse(localStorage.getItem('userinfo')).id
      shopcar_sub({ userid, goodid: payload.id }).then((res) => {
        if (res.data.code == 200) {
          store.commit('sub_shopcar', payload)
        }
      })
    },
    // 发送编辑地址的请求
    request_edit(store, payload) {
      var userid = JSON.parse(localStorage.getItem("userinfo")).id
      address_edit({ userid, addressid: payload.addressid, address: payload.address, phone: payload.phone, name: payload.name }).then((res) => {
        // console.log(res.data)
        // console.log(userid, payload.address, payload.addressid, payload.name)
        if (res.data.code == 200) {
          store.commit('edit_address', payload)
        }

      })
    }


  },
  modules: {
  },
  plugins: [createLogger(), createPersistedState()]
})
